package com.whj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whj.domain.constants.SystemConstants;
import com.whj.domain.dto.TagDTO;
import com.whj.domain.entity.Tag;
import com.whj.domain.response.ResponseResult;
import com.whj.domain.vo.PageVo;
import com.whj.domain.vo.TagVo;
import com.whj.mapper.TagMapper;
import com.whj.service.TagService;
import com.whj.utils.BeanCopyUtils;
import com.whj.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 标签(Tag)表服务实现类
 *
 * @author wanghaijun
 * @since 2023-05-30 09:28:12
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Autowired
    private TagMapper tagMapper;

    /**
     * 查询标签，平且进行分页
     *
     * @param pageNum
     * @param pageSize
     * @param tagDTO
     * @return
     */
    @Override
    public PageVo pageTagList(Integer pageNum, Integer pageSize, TagDTO tagDTO) {
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getCreateBy,userId);
        queryWrapper.eq(Tag::getDelFlag, SystemConstants.DELETE_TAG_NOMAL);
        queryWrapper.like(StringUtils.hasText(tagDTO.getName()), Tag::getName, tagDTO.getName());
        queryWrapper.like(StringUtils.hasText(tagDTO.getRemark()), Tag::getRemark, tagDTO.getRemark());
        Page<Tag> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);
        return new PageVo(page.getRecords(), page.getTotal());
    }

    /**
     * 新增标签
     *
     * @param tagDTO
     * @return
     */
    @Override
    public ResponseResult addTag(TagDTO tagDTO) {
        Long userId = SecurityUtils.getUserId();
        String remark = tagDTO.getRemark();
        String name = tagDTO.getName();
        Tag tag = new Tag();
        tag.setCreateBy(userId);
        tag.setName(name);
        tag.setRemark(remark);
        tagMapper.insert(tag);
        return ResponseResult.okResult();
    }

    /**
     * 删除标签
     *
     * @param id 删除id
     * @return
     */
    @Override
    public ResponseResult<TagDTO> deleteTag(List<String> id) {

        List<Long> collect = id.stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        tagMapper.deleteBatchIds(collect);
        return ResponseResult.okResult();
    }

    /**
     * 修改标签
     *
     * @param tagDTO
     * @return
     */
    @Override
    public ResponseResult updateTag(TagDTO tagDTO) {

        Tag tag = tagMapper.selectById(tagDTO.getId());
        tag.setName(tagDTO.getName());
        tag.setRemark(tagDTO.getRemark());
        tagMapper.updateById(tag);
        return ResponseResult.okResult();
    }

    /**
     * 查询单个标签
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult<TagDTO> tag(Long id) {
        Tag tag = tagMapper.selectById(id);
        return ResponseResult.okResult(new TagDTO(id, tag.getName(), tag.getRemark()));
    }


    /**
     * 查询所有标签
     *
     * @return
     */
    @Override
    public List<TagVo> listAllTag() {
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Tag::getId,Tag::getName);
        List<Tag> list = list(wrapper);
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(list, TagVo.class);
        return tagVos;
    }

}
